package com.smec.remes.module.register.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.smec.moblie.BR;
import com.smec.moblie.R;
import com.smec.moblie.databinding.LayoutNullDataBinding;
import com.smec.moblie.databinding.LayoutRegisterBinding;
import com.smec.remes.base.SmecBaseRecyclerViewHolder;
import com.smec.remes.bean.AbleMatnr;
import com.smec.remes.util.CommonUtils;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/18.
 */

public class SmecRegisterAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder>{

    private ArrayList<AbleMatnr> arrayList=new ArrayList<>();
    private Context mcontext;
    private static int  NULLDATA = 0;
    private static int  DATA = 1;

    public SmecRegisterAdapter(Context context) {
        this.mcontext=context;
    }

    public void setList(ArrayList<AbleMatnr> list){
        if(!CommonUtils.notEmpty(list)){
            return;
        }
        if(!CommonUtils.notEmpty(arrayList)){
            arrayList=list;
            notifyDataSetChanged();
            return;
        }
        for(int i=arrayList.size()-1;i>=0;i--){
            String matnr1=arrayList.get(i).getMatnr();
            for(int j=0;j<list.size();j++){
                String matnr2=list.get(j).getMatnr();
                if(matnr1.equals(matnr2)){
                    arrayList.remove(i);
                }
            }
        }
        arrayList.addAll(list);
        notifyDataSetChanged();
    }

    public void setArrayList(ArrayList<AbleMatnr> arrayList) {
        this.arrayList = arrayList;
        notifyDataSetChanged();
    }

    public void clearList(){
        arrayList.clear();
        notifyDataSetChanged();
    }

    public  ArrayList<AbleMatnr> _getList(){
        return arrayList;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == DATA) {
            LayoutRegisterBinding layoutRecyclerviewRegisterBinding = DataBindingUtil.inflate
                    (LayoutInflater.from(parent.getContext()), R.layout.layout_register, parent, false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder = new
                    SmecBaseRecyclerViewHolder(layoutRecyclerviewRegisterBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutRecyclerviewRegisterBinding);
            return smecBaseRecyclerViewHolder;
        }else {
            LayoutNullDataBinding layoutNullDataBinding = DataBindingUtil.inflate
                    (LayoutInflater.from(parent.getContext()), R.layout.layout_null_data,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutNullDataBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutNullDataBinding);
            return smecBaseRecyclerViewHolder;

        }
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, final int position) {
        if(holder.getViewType() == DATA) {
            final LayoutRegisterBinding layoutRecyclerviewRegisterBinding = (LayoutRegisterBinding) holder.getBinding();
            layoutRecyclerviewRegisterBinding.setVariable(BR.register, arrayList.get(position));
            layoutRecyclerviewRegisterBinding.ivItemadd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    arrayList.remove(position);
                    notifyDataSetChanged();
                }
            });
        }
    }

    //getItemViewType方法是用来获取当前项Item(position参数)是哪种类型的布局
    @Override
    public int getItemViewType(int position) {
        //在这里设置onCreateViewHolde方法内的viewType参数，然后根据这个参数来设置什么样的布局，是占位图还是显示数据的布局
        if(arrayList == null || arrayList.size() == 0){
            return NULLDATA;
        }else{
            return DATA;
        }
    }


    @Override
    public int getItemCount() {
        if(arrayList.size() == 0){
            return 1;
        }else{
            return arrayList.size();
        }

    }
}
